package com.erp.util;

import android.util.Log;

import com.erp.bean.ProdyctionClientBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/26/026.
 */
public class JsonUtil {
    public static ProdyctionClientBean JsonProdyction(String data) {
        ProdyctionClientBean prodyctionClientBean=new ProdyctionClientBean();
        try {
            JSONObject jo = new JSONObject(data);
            prodyctionClientBean.crm_oid=getvolue(jo,"crm_oid");
            prodyctionClientBean.id=getvolue(jo,"id");
            prodyctionClientBean.customer=getvolue(jo,"customer");
            prodyctionClientBean.content_description=getvolue(jo,"content_description");
            prodyctionClientBean.special_status=getvolue(jo,"special_status");
            prodyctionClientBean.shop_name=getvolue(jo,"shop_name");
            prodyctionClientBean.ot_name=getvolue(jo,"ot_name");
            prodyctionClientBean.cd_time=getvolue(jo,"cd_status");
            prodyctionClientBean.onum=getvolue(jo,"onum");
            prodyctionClientBean.contract_num=getvolue(jo,"contract_num");
            prodyctionClientBean.sqxd_uid=getvolue(jo,"sqxd_uid_name");
            prodyctionClientBean.sqxd_time=getvolue(jo,"sqxd_time");
            prodyctionClientBean.cd_status=getvolue(jo,"cd_status");
            prodyctionClientBean.cd_acceptance_time=getvolue(jo,"cd_acceptance_time");
            prodyctionClientBean.dingbaoyuan=getvolue(jo,"dingbaoyuan_name");
            prodyctionClientBean.dingbao_time=getvolue(jo,"dingbao_time");
            prodyctionClientBean.audit_time=getvolue(jo,"audit_time");
            prodyctionClientBean.charge_auditor=getvolue(jo,"charge_auditor_name");
            prodyctionClientBean.auditor=getvolue(jo,"auditor_name");
            prodyctionClientBean.charge_audit_time=getvolue(jo,"charge_audit_time");
            prodyctionClientBean.warehousing_time=getvolue(jo,"warehousing_time");
            prodyctionClientBean.deliver_time=getvolue(jo,"deliver_time");
            prodyctionClientBean.ctime=getvolue(jo,"ctime");
            prodyctionClientBean.remark=getvolue(jo,"remark");
            prodyctionClientBean.all_package_num=getvolue(jo,"all_package_num");
            prodyctionClientBean.s_package_num=getvolue(jo,"s_package_num");
            prodyctionClientBean.m_package_num=getvolue(jo,"m_package_num");
            prodyctionClientBean.rs_package_num=getvolue(jo,"rs_package_num");
            prodyctionClientBean.cabinet_counts=getvolue(jo,"cabinet_counts");
            prodyctionClientBean.warehoused_num=getvolue(jo,"warehoused_num");
            prodyctionClientBean.delivered_num=getvolue(jo,"delivered_num");
            prodyctionClientBean.quoted_price_yuan_name=getvolue(jo,"quoted_price_yuan_name");
            prodyctionClientBean.quoted_price_time=getvolue(jo,"quoted_price_time");
            prodyctionClientBean.productive_time=getvolue(jo,"productive_time");
            prodyctionClientBean.oid=getvolue(jo,"oid");
            JSONArray jsqingdan=getjsvolue(jo,"deliver_list");
            List<String> listqingdan=new ArrayList<>();
            if(jsqingdan!=null){
                for(int i=0;i<jsqingdan.length();i++){
                    String s=jsqingdan.getString(i);
                    listqingdan.add(s);
                }
            }
            prodyctionClientBean.deliver_list=listqingdan;
            List<String> mlist = new ArrayList<>();
            if(getisjo(jo,"deliver_voucher")) {
                JSONArray jss = jo.getJSONArray("deliver_voucher");
                for (int i = 0; i < jss.length(); i++) {
                    mlist.add((String) jss.get(i));
                }
            }
            prodyctionClientBean.deliver_voucher=mlist;
            ArrayList<ProdyctionClientBean.guimingxi> guimingxiList=new ArrayList<>();
            if(getisjo(jo,"cabinet")) {
                JSONArray js1 = (JSONArray) jo.getJSONArray("cabinet");
                for (int i = 0; i < js1.length(); i++) {
                    JSONObject jo1= (JSONObject) js1.get(i);
                    ProdyctionClientBean.guimingxi guimingxi=new ProdyctionClientBean.guimingxi();
                    guimingxi.guihao=getvolue(jo1,"cnum");
                    guimingxi.chaidanyuan=getvolue(jo1,"dismantler_name");
                    guimingxi.chaidanshijian=getvolue(jo1,"c_dismantle_time");
                    guimingxi.rucangshijian=getvolue(jo1,"c_warehousing_time");
                    guimingxi.fahuoshijian=getvolue(jo1,"c_deliver_time");
                    guimingxi.zhuangtai=getvolue(jo1,"status_name");
                    JSONArray jsguitu=getjsvolue(jo1,"tz_png_array");
                    ArrayList<String> tuzhis=new ArrayList<>();
                    for(int t=0;t<jsguitu.length();t++){
                        tuzhis.add(jsguitu.get(t).toString());
                    }
                    guimingxi.tuzhis=tuzhis;
                    guimingxi.id=getvolue(jo1,"id");
                    guimingxi.name=getvolue(jo1,"name");
                    guimingxi.cte_time=getvolue(jo1,"cte_time");
                    guimingxi.cte_problem=getvolue(jo1,"cte_problem");
                    guimingxi.cte_time=getvolue(jo1,"cte_time");
                    guimingxi.cte_problem=getvolue(jo1,"cte_problem");
                    guimingxiList.add(guimingxi);
                }
            }
            prodyctionClientBean.guimingx=guimingxiList;
            ArrayList<ProdyctionClientBean.baomingxi> baomingxiList=new ArrayList<>();
            if(getisjo(jo,"package")){
                JSONArray js1 = (JSONArray) jo.get("package");
                for (int i = 0; i < js1.length(); i++) {
                    JSONObject jo2= (JSONObject) js1.get(i);
                    ProdyctionClientBean.baomingxi baomingxi=new ProdyctionClientBean.baomingxi();
                    baomingxi.guihao=getvolue(jo2,"cnum");
                    baomingxi.baohao=getvolue(jo2,"pnum");
                    baomingxi.zhuangtai=getvolue(jo2,"status_name");
                    baomingxi.rucangshijian=getvolue(jo2,"p_warehousing_time");
                    baomingxi.fahuoshijian=getvolue(jo2,"p_deliver_time");
                    baomingxi.rucangyuan=getvolue(jo2,"warehouser_name");
                    baomingxi.fahuoyuan=getvolue(jo2,"delivor_name");
                    baomingxi.leixing=getvolue(jo2,"pt_name");
                    baomingxi.position=getvolue(jo2,"position");
                    baomingxi.checi=getvolue(jo2,"dc_deliver_car_number");
                    baomingxi.tiaoxingma=getvolue(jo2,"p_code");
                    baomingxi.beizhu=getvolue(jo2,"p_remark");
                    baomingxi.material=getvolue(jo2,"material");
                    baomingxi.position_buffer=getvolue(jo2,"pallet_position_buffer");
                    baomingxi.height=getvolue(jo2,"height");
                    baomingxi.zp_ctime=getvolue(jo2,"zp_ctime");
                    baomingxi.pallet_num=getvolue(jo2,"pallet_num");
                    baomingxiList.add(baomingxi);
                }
            }
            prodyctionClientBean.baomingxi=baomingxiList;
            ArrayList<ProdyctionClientBean.wuliaomingxi> wuliaomingxis=new ArrayList<>();
            if(getisjo(jo,"material")){
                JSONArray js1=jo.getJSONArray("material");
                addwuliaos(wuliaomingxis, js1);
            }
            prodyctionClientBean.wuliaomingxis=wuliaomingxis;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return prodyctionClientBean;
    }

    public static void addwuliaos(ArrayList<ProdyctionClientBean.wuliaomingxi> wuliaomingxis, JSONArray js1) throws JSONException {
        Log.d("addwuliaos","============="+js1.toString());
        for(int i=0;i<js1.length();i++){
            JSONObject jo2= (JSONObject) js1.get(i);
            ProdyctionClientBean.wuliaomingxi wuliaomingxi=new ProdyctionClientBean.wuliaomingxi();
            wuliaomingxi.c_cnum=getvolue(jo2,"c_cnum");
            wuliaomingxi.p_pnum=getvolue(jo2,"p_pnum");
            wuliaomingxi.mc1_mark=getvolue(jo2,"mc1_mark");
            wuliaomingxi.mc2_mark=getvolue(jo2,"mc2_mark");
            wuliaomingxi.mc_2_name=getvolue(jo2,"mc_2_name");
            wuliaomingxi.m_name=getvolue(jo2,"m_name");
            wuliaomingxi.es_type=getvolue(jo2,"es_type");
            wuliaomingxi.m_size=getvolue(jo2,"m_size");
            wuliaomingxi.color_color=getvolue(jo2,"color_color");
            wuliaomingxi.color_code=getvolue(jo2,"color_code");
            wuliaomingxi.m_counts=getvolue(jo2,"m_counts");
            wuliaomingxi.unit_unit=getvolue(jo2,"unit_unit");
            wuliaomingxi.m_remark=getvolue(jo2,"m_remark");
            wuliaomingxi.m_code=getvolue(jo2,"m_code");
            wuliaomingxi.m_DXF=getvolue(jo2,"m_DXF");
            wuliaomingxi.m_square=getvolue(jo2,"m_square");
            wuliaomingxi.m_material_num=getvolue(jo2,"m_material_num");
            wuliaomingxi.m_thickness=getvolue(jo2,"m_thickness");
            wuliaomingxi.mb_weihao=getvolue(jo2,"mb_weihao");
            wuliaomingxi.m_fangxiang=getvolue(jo2,"m_fangxiang");
            wuliaomingxi.m_small_or_big=getvolue(jo2,"m_small_or_big");
            wuliaomingxi.mb_batch=getvolue(jo2,"mb_batch");
            wuliaomingxi.m_category=getvolue(jo2,"m_category");
            wuliaomingxi.oo_onum=getvolue(jo2,"oo_onum");
            wuliaomingxi.cqi_name=getvolue(jo2,"cqi_name");
            wuliaomingxi.cqi_mark=getvolue(jo2,"cqi_mark");
            wuliaomingxi.co_shouliren=getvolue(jo2,"co_shouliren");
            wuliaomingxi.co_shouli_time=getvolue(jo2,"co_shouli_time");
            wuliaomingxi.cor_name=getvolue(jo2,"cor_name");
            wuliaomingxi.ci_name=getvolue(jo2,"ci_name");
            wuliaomingxi.area_num=getvolue(jo2,"area_num");
            wuliaomingxi.fangxiang=getvolue(jo2,"fangxiang");
            wuliaomingxi.finished_size=getvolue(jo2,"finished_size");
            wuliaomingxi.slot_name=getvolue(jo2,"slot_name");
            wuliaomingxi.mm_menjiaokong=getvolue(jo2,"mm_menjiaokong");
            wuliaomingxis.add(wuliaomingxi);
        }
    }

    public static String getvolue(JSONObject jo,String key) throws JSONException {
        if(jo.has(key)){
            String s=jo.getString(key);
            if(s.equals("null")){
                return "";
            }else{
                return s;
            }
        }else {
            return "";
        }
    }
    public static int getint(JSONObject jo,String key) throws JSONException {
        if(jo.has(key)){
            return jo.getInt(key);
        }else {
            return -1;
        }
    }
    public static boolean getisvolue(JSONObject jo,String key) throws JSONException {
        if(jo.has(key)){
            return jo.getBoolean(key);
        }else{
            return false;
        }
    }
    public static JSONArray getjsvolue(JSONObject jo,String key) throws JSONException {
        if(getisjo(jo,key)){
            return jo.getJSONArray(key);
        }else{
            return new JSONArray();
        }
    }
    public static JSONObject getjovolue(JSONObject jo,String key) throws JSONException {
        if(getisjo(jo,key)){
            return jo.getJSONObject(key);
        }else{
            return new JSONObject();
        }
    }
    public static boolean getisjo(JSONObject jo,String key) throws JSONException {
        if(jo.has(key)){
            if(jo.getString(key).equals("") || jo.getString(key).equals("null")){
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }
    public static void toost(String data){
        try{
            JSONArray js=new JSONArray(data);
            Myutil.MyTost(js.getString(0));
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}
